#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import yaml
import argparse
import sys
import os

def generate_summary(custom_resolved_file):
    """
    解析 custom_resolved.yml 文件并生成组件安装总结报告
    """
    if not os.path.exists(custom_resolved_file):
        print(f"错误: 文件 {custom_resolved_file} 不存在")
        return False

    try:
        with open(custom_resolved_file, 'r') as file:
            data = yaml.safe_load(file)
    except Exception as e:
        print(f"解析 YAML 文件时出错: {e}")
        return False

    # 定义组件信息提取逻辑
    components = []

    # Nginx
    if 'nginx_ip' in data and 'nginx_port' in data:
        components.append({
            'name': 'Nginx',
            'ip': data.get('nginx_ip', '-'),
            'port': data.get('nginx_port', '-'),
            'user': data.get('nginx_run_user', '-'),
            'password': '-'
        })

    # Nacos
    if 'nacos_ip' in data and 'nacos_port' in data:
        components.append({
            'name': 'Nacos',
            'ip': data.get('nacos_ip', '-'),
            'port': data.get('nacos_port', '-'),
            'user': 'nacos',
            'password': data.get('nacos_pwd', '-')
        })

    # MinIO
    if 'minio_ip' in data and 'minio_console_port' in data:
        components.append({
            'name': 'MinIO',
            'ip': data.get('minio_ip', '-'),
            'port': data.get('minio_console_port', '-'),
            'user': data.get('minio_access_key', '-'),
            'password': data.get('minio_secret_key', '-')
        })

    # MySQL
    if 'mysql_ip' in data and 'mysql_port' in data:
        components.append({
            'name': 'MySQL',
            'ip': data.get('mysql_ip', '-'),
            'port': data.get('mysql_port', '-'),
            'user': data.get('mysql_db_user', '-'),
            'password': data.get('mysql_db_pwd', '-')
        })

    # Redis
    if 'redis_ip' in data and 'redis_port' in data:
        components.append({
            'name': 'Redis',
            'ip': data.get('redis_ip', '-'),
            'port': data.get('redis_port', '-'),
            'user': '-',
            'password': data.get('redis_pwd', '-')
        })

    # Zookeeper
    if 'zookeeper_ip' in data and 'zk_port' in data:
        components.append({
            'name': 'Zookeeper',
            'ip': data.get('zookeeper_ip', '-'),
            'port': data.get('zk_port', '-'),
            'user': '-',
            'password': '-'
        })

    # Kafka
    if 'kafka_ip' in data and 'kafka_port' in data:
        components.append({
            'name': 'Kafka',
            'ip': data.get('kafka_ip', '-'),
            'port': data.get('kafka_port', '-'),
            'user': '-',
            'password': '-'
        })

    # Elasticsearch
    if 'es_node1_ip' in data and 'es_http_port' in data:
        components.append({
            'name': 'Elasticsearch',
            'ip': data.get('es_node1_ip', '-'),
            'port': data.get('es_http_port', '-'),
            'user': data.get('es_xpack_user', '-'),
            'password': data.get('es_xpack_pwd', '-')
        })

    # Kibana
    if 'kibana_ip' in data and 'kibana_port' in data:
        components.append({
            'name': 'Kibana',
            'ip': data.get('kibana_ip', '-'),
            'port': data.get('kibana_port', '-'),
            'user': data.get('kibana_es_username', '-'),
            'password': data.get('kibana_es_password', '-')
        })

    # ClickHouse
    if 'ck_http_port' in data:
        components.append({
            'name': 'ClickHouse',
            'ip': data.get('mysql_ip', '-'),  # 使用MySQL IP作为默认
            'port': data.get('ck_http_port', '-'),
            'user': 'default',
            'password': data.get('ck_default_password', '-')
        })

    # SFTP
    if 'sftp_ip' in data and 'sftp_port' in data:
        components.append({
            'name': 'SFTP',
            'ip': data.get('sftp_ip', '-'),
            'port': data.get('sftp_port', '-'),
            'user': data.get('sftp_user', '-'),
            'password': data.get('sftp_pwd', '-')
        })

    # 输出表格格式
    print(f"{'组件':<15} {'IP地址':<15} {'端口':<8} {'用户名':<12} {'密码':<15}")
    print("="*65)

    for comp in components:
        print(f"{comp['name']:<15} {comp['ip']:<15} {comp['port']:<8} {comp['user']:<12} {comp['password']:<15}")

    print("")
    print("注：部分密码可能已加密（以ENC开头），请使用相应解密工具获取明文")
    
    return True

def main():
    parser = argparse.ArgumentParser(description='生成组件安装总结报告')
    parser.add_argument('custom_file', help='custom_resolved.yml 文件路径')
    
    args = parser.parse_args()
    
    print("==================== 组件安装总结 ====================")
    print("")
    print("已安装的组件运行信息：")
    print("")
    
    success = generate_summary(args.custom_file)
    
    if success:
        print("")
        print("======================================================")
        sys.exit(0)
    else:
        sys.exit(1)

if __name__ == "__main__":
    main()
